<?php

//操作日志写入数据库

namespace listen;
use app\admin\model\Admin\Log as LogModel;

class DoLog{

    public function handle($user){
        // 默认只记录增加，修改，删除操作
        if(in_array(request()->action(),['add','update','delete','index'])){
			$content = request()->except(['s', '_pjax']);
			if ($content) {
				foreach ($content as $k => $v) {
					if ((is_string($v) && strlen($v) > 200) || stripos($k, 'password') !== false) {
						unset($content[$k]);
					}
				}
			}

            $ip = request()->ip();
            $data['ip'] = $ip;
            $data['city'] = \utils\IpAddress::getIpInfo($ip);

			$data['application_name'] = app('http')->getName();
			$data['username'] = $user['username'];
            $data['creater_id'] = $user['user_id'];
            $data['creater_dept'] = $user['dept_id'];
			$data['url'] = request()->url(true);
			$data['useragent'] = request()->server('HTTP_USER_AGENT');
			$data['content'] = json_encode($content,JSON_UNESCAPED_UNICODE);
			$data['create_time'] = date('Y-m-d H:i:s');
			$data['type'] = 2;
			$data['status'] = 1;

			$loginfo = LogModel::create($data);
            if($loginfo->id){
                LogModel::update(['sort_id'=>$loginfo->id,'id'=>$loginfo->id]);
            }
		}

    }
}
